package com.hxs.colorfultest2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.thymeleaf.spring5.SpringTemplateEngine;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyCustomAuthenticationFilter myCustomAuthenticationFilter;

    @Bean
    UserDetailsService customUserService() { //注册UserDetailsService 的bean
        return new CustomerService();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService()).passwordEncoder(
                new PasswordEncoder() {
                    @Override
                    public String encode(CharSequence rawPassword) {
                        return (String) rawPassword;
                    }

                    @Override
                    public boolean matches(CharSequence rawPassword, String encodedPassword) {
                        if (encodedPassword.equals(rawPassword)) {
                            return true;
                        } else {
                            throw new BadCredentialsException("密码错误！");
                        }
                    }
                }

        ); //user Details Service验证


    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
              /*  .antMatchers( "/login", "/defaultKaptcha", "/index","/main","/test").permitAll()
                .anyRequest().authenticated()*/
                .and().formLogin()
                .loginPage("/login")
                //设置默认登录成功跳转页面,错误回到login界面
                .defaultSuccessUrl("/index").failureUrl("/login?error=true").permitAll()
                .and()
                .logout().logoutSuccessUrl("/login");
        http.addFilterBefore(myCustomAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

      /*  // 只需要以下配置即可启用记住密码
        http.authorizeRequests()
                .and()
                .rememberMe().;*/
    }

    //解决静态资源被拦截的问题
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("**/*.gif","/**/*.js", "/lang/*.json", "/**/*.css", "/**/*.map", "/**/*.png", "/**/*.jpg", "/**/*.jpg");
    }

   /* @Bean
    public SpringTemplateEngine templateEngine(){

        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);

        // add dialect spring security
        templateEngine.addDialect(new SpringSecurityDialect());
        return templateEngine;
    }*/
}
